package com.talkweb.newproject.admin.rest;

import com.talkweb.newproject.admin.sysrolemapping.dto.SysRoleMappingDTO;
import com.talkweb.newproject.admin.sysrolemapping.service.SysRoleMappingService;
import com.talkweb.newproject.admin.sysroleuser.dto.SysRoleUserDTO;
import com.talkweb.newproject.admin.sysroleuser.service.SysRoleUserService;
import com.talkweb.newproject.api.model.admin.SysRoleUserParam;
import com.talkweb.newproject.api.model.common.Response;
import com.talkweb.newproject.api.model.common.ResponseResult;
import com.talkweb.newproject.api.model.common.Status;
import lombok.extern.slf4j.Slf4j;
import org.bouncycastle.cert.ocsp.Req;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created by qsf on 2019/5/9.
 */
@RestController
@RequestMapping(value="/api/sysRoleUser")
@Slf4j
public class SysRoleUserRest {

    @Autowired
    private SysRoleUserService sysRoleUserService;

    /**
     * 根据id查询对象
     * @param id
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value="get/v1/{id}",method = RequestMethod.POST)
    public ResponseResult<SysRoleUserDTO> querySysRoleUserByPrimaryKey(@PathVariable("id") String id) throws Exception {
        ResponseResult<SysRoleUserDTO>  result=new ResponseResult<>();
        try{
            SysRoleUserDTO sysRoleUserDTO = sysRoleUserService.querySysRoleUserByPrimaryKey(id);
            result.setStatus(Status.SUCCESS);
            result.setData(sysRoleUserDTO);
            result.setMessage("查询成功");
            return result;
        }catch(Exception e) {
            log.error(e.getMessage(),e);
            result.setStatus(Status.FAILD);
            result.setMessage("执行异常,请重试");
            return result;
        }
    }

    /**
     * 取得list对象
     * @param params
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value="/querySysRoleUserList",method = RequestMethod.POST)
    public Object querySysRoleUserList(@RequestBody SysRoleUserParam params) throws Exception {
        return sysRoleUserService.searchSysRoleUser(params);
    }

    /**
     * 根据条件查询SysRoleUser对象
     * @param dto
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value="/queryLike",method=RequestMethod.POST)
    public ResponseResult<SysRoleUserDTO> queryLike(@RequestBody SysRoleUserDTO dto) throws Exception {
        ResponseResult<SysRoleUserDTO> result = new ResponseResult<>();
        try{
            SysRoleUserDTO sysRoleUserDTO = sysRoleUserService.queryLikeSysRoleUser(dto);
            result.setStatus(Status.SUCCESS);
            result.setData(sysRoleUserDTO);
            result.setMessage("查询成功!");
            return result;
        }catch(Exception e) {
            log.error(e.getMessage(),e);
            result.setStatus(Status.FAILD);
            result.setMessage("执行异常,请重试");
            return result;
        }
    }

    /**
     * 新增数据
     * @param dto
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value="/create/v1",method= RequestMethod.POST)
    public ResponseResult create (@RequestBody SysRoleUserDTO dto) throws Exception {
        ResponseResult result = new ResponseResult();
        try{
            sysRoleUserService.inserSysRoleUser(dto);
            result.setStatus(Status.SUCCESS);
            result.setMessage("添加成功");
            return result;
        }catch(Exception e) {
          log.error(e.getMessage(),e);
          result.setStatus(Status.FAILD);
          result.setMessage("执行异常,请重试");
          return result;
        }
    }

    /**
     * 更新数据
     * @param dto
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value="/update/v1",method = RequestMethod.POST)
    public ResponseResult update(@RequestBody SysRoleUserDTO dto) throws Exception {
        ResponseResult result = new ResponseResult();
        try{
            sysRoleUserService.updateSysRoleUser(dto);
            result.setStatus(Status.SUCCESS);
            result.setMessage("更新成功");
            return result;
        }catch(Exception e) {
            log.error(e.getMessage(),e);
            result.setStatus(Status.FAILD);
            result.setMessage("执行异常,请重试");
            return result;
        }
    }

    /**
     * 根据id删除数据
     * @param id
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value="/delete/v1",method = RequestMethod.POST)
    public ResponseResult deleteSysRoleUserById(@RequestParam("id") String id) throws Exception {
        ResponseResult result = new ResponseResult();
        try{
            boolean isDel = sysRoleUserService.deleteSysRoleUserById(id);
            if(isDel) {
                result.setStatus(Status.SUCCESS);
                result.setMessage("删除成功");
            }else {
                result.setStatus(Status.FAILD);
                result.setMessage("删除失败");
            }
            return result;
        }catch(Exception e) {
            log.error(e.getMessage());
            result.setStatus(Status.FAILD);
            result.setMessage("执行异常,请重试");
            return result;
        }
    }



}
